#include "dino.h"
#include "pic.h"

uint8_t f_jumpCompleted = 1;
uint8_t f_gameOver = 0;
uint8_t height = 0;
int16_t pos_cloud = 130;
int16_t pos_cactus = 148;
uint8_t xy_dino[8];
int8_t num_Heightarr = 8;
uint8_t direction = 1;
uint16_t scored = 0;

void parameter_init(void)
{
	f_jumpCompleted = 1;
	pos_cloud = 130;
	pos_cactus = 148;
	num_Heightarr = 8;
	direction = 1;
	scored = 0;
}

void cloud_show(void)
{
//	static int16_t pos = 130;
	LCD_Fill(pos_cloud+3,10,pos_cloud+28,20,WHITE);
	if(pos_cloud < 0) pos_cloud = 130;
	LCD_ShowPicture(pos_cloud,10,25,10,cloud);
	pos_cloud = pos_cloud - 3;
}
void dino_show(void)
{
	static uint8_t num = 1;
	height = 0;
	if(num == 1) LCD_ShowPicture(20,53,23,25,dino1);
	else LCD_ShowPicture(20,53,23,25,dino2);
	num = (num==1?0:1);
	xy_dino[0] = 20;
	xy_dino[1] = 2;
	xy_dino[2] = 43;
	xy_dino[3] = 2;
	xy_dino[4] = 20;
	xy_dino[5] = 26;
	xy_dino[6] = 43;
	xy_dino[7] = 26;
}

void cactus_show(void)
{
//	static int16_t pos = 148;
	uint8_t X_min,X_max,Y_min,Y_max;
	LCD_Fill(pos_cactus+2,61,pos_cactus+10,80,WHITE);
	if(pos_cactus < 0) pos_cactus = 148;
	LCD_ShowPicture(pos_cactus,61,7,17,cactus_small);
	pos_cactus = pos_cactus - 3;
	X_min = pos_cactus+2;
	X_max = pos_cactus+10;
	Y_min = 0;
	Y_max = 15;
	for(uint8_t j=0;j<4;j++)
	{
		if((xy_dino[2*j] >= X_min) && (xy_dino[2*j] <= X_max))
		{
			if((xy_dino[2*j+1] >= Y_min) && (xy_dino[2*j+1] <= Y_max)) f_gameOver = 1;
		}
	}
	
}
	
void dino_Jump(void)
{
	f_jumpCompleted = 0;
	static uint8_t num_trip = 1;

//	static int8_t arr_num = 8;
	uint8_t arr_height[] = {1, 1, 3, 3, 4, 4, 5, 6, 7};
	
	if(direction) 
	{
		LCD_Fill(20,72-height,40,80-height,WHITE);
		height = arr_height[num_Heightarr] + height;
		num_Heightarr--;
		if(num_Heightarr < 0) 
		{
			direction = 0;
			num_Heightarr = 0;
		}
	}
	else 
	{
		LCD_Fill(25,50-height,43,60-height,WHITE);
		height = height - arr_height[num_Heightarr];
		num_Heightarr++;
		if(num_Heightarr > 8) 
		{
			direction = 1;
			num_Heightarr = 8;
			scored++;
			LCD_ShowIntNum(130,36,scored,3,RED,WHITE,16);
			f_jumpCompleted = 1;
		}
	}
	if(num_trip == 1) LCD_ShowPicture(20,53-height,23,25,dino1);
	else LCD_ShowPicture(20,53-height,23,25,dino2);
	num_trip = (num_trip==1?0:1);
	xy_dino[0] = 20;
	xy_dino[1] = 4+height;
	xy_dino[2] = 43;
	xy_dino[3] = 4+height;
	xy_dino[4] = 20;
	xy_dino[5] = 29+height;
	xy_dino[6] = 43;
	xy_dino[7] = 29+height;
	
	
}